package com.tma.mobilechart;

import com.tma.mobilechart.utilitys.ScreenShot;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.ImageView;

public class ShareActivity extends Activity {
    /** Called when the activity is first created. */
	
	private float x1,x2;
	private Activity activity1;
	private ImageView imgScreen;
	private Bitmap image;
	private String path;
	
	private static final int SHARE_ID = Menu.FIRST;	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_share);   
        
        activity1 = this;       
        path = RenderLineActivity.pathChart;
        
        imgScreen = (ImageView)findViewById(R.id.imgScreen);
        imgScreen.setImageBitmap(RenderLineActivity.mChart);        
    }  
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
	     // TODO Auto-generated method stub      
	    
	     int action = event.getAction();
	     switch(action){
	     	case MotionEvent.ACTION_DOWN:       		
	     		x1 = event.getX();
	     		break;
	     	case MotionEvent.ACTION_MOVE:       		
	     		break;
	     	case MotionEvent.ACTION_UP: 
	     		x2 = event.getX();
	     		if(Math.abs(x1 - x2)>100)
	     		{
	     			image = ScreenShot.takeScreenShot(activity1);
	     			imgScreen.setImageBitmap(image);
	     			path = ScreenShot.savePic(image);	     			
	     		}
	     		break;
	     	case MotionEvent.ACTION_CANCEL:     
	     		break;
	     	case MotionEvent.ACTION_OUTSIDE:     
	     		break;
	     	default:
	     		break;
	     	}
	     return true; //processed
    }  
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);		
		menu.add(Menu.NONE, SHARE_ID, Menu.NONE, R.string.menu_share).setIcon(
				android.R.drawable.ic_menu_share);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
		switch (item.getItemId()) {
		case SHARE_ID:
			Intent sharingIntent = new Intent(Intent.ACTION_SEND);
			Uri screenshotUri = Uri.parse(path);
			sharingIntent.setType("image/png");
			sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
			startActivity(Intent.createChooser(sharingIntent, "Share Image"));
            break;
		default:
			return super.onOptionsItemSelected(item);
		}
		return true;
	}
            
}